package com.power.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.power.domain.MemberAddr;
import com.power.mapper.MemberAddrMapper;
import com.power.service.MemberAddrService;
import com.power.util.AuthUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author dingchunlin
* @description 针对表【member_addr(用户配送地址)】的数据库操作Service实现
* @createDate 2024-07-10 15:12:14
*/
@Service
@CacheConfig(cacheNames = "com.power.service.impl.MemberAddrServiceImpl")
public class MemberAddrServiceImpl extends ServiceImpl<MemberAddrMapper, MemberAddr> implements MemberAddrService{

    @Autowired
    private MemberAddrMapper memberAddrMapper;

    /**
     * 查询所有用户配送地址
     * @return
     */
    @Override
    @Cacheable(key = "'queryAllMemberAddr'")
    public List<MemberAddr> queryAllMemberAddr() {
        // 获取当前用户的openid
        String openId = AuthUtils.getMemberOpenId();
        // 根据openid查询用户配送地址
        List<MemberAddr> memberAddrList = memberAddrMapper.selectList(new LambdaQueryWrapper<MemberAddr>()
                .eq(MemberAddr::getOpenId, openId)
                .eq(MemberAddr::getStatus, 1)
                .orderByDesc(MemberAddr::getCommonAddr, MemberAddr::getCreateTime)
        );
        return memberAddrList;
    }

    /**
     * 修改默认地址
     * @param addrId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(key = "'queryAllMemberAddr'")
    public Boolean modifyDefaultAddr(Long addrId) {
        // 获取当前用户的openid
        String openId = AuthUtils.getMemberOpenId();
        // 先将所有的地址变成非默认地址
        MemberAddr memberAddr = new MemberAddr();
        memberAddr.setCommonAddr(0);
        memberAddrMapper.update(memberAddr, new LambdaQueryWrapper<MemberAddr>()
                .eq(MemberAddr::getOpenId, openId)
        );
        // 再将当前地址设置为默认地址
        MemberAddr addr = new MemberAddr();
        addr.setAddrId(addrId);
        addr.setCommonAddr(1);
        addr.setUpdateTime(new Date());
        return memberAddrMapper.updateById(addr) == 1;
    }

    /**
     * 保存配送地址
     * @param memberAddr
     * @return
     */
    @Override
    @CacheEvict(key = "'queryAllMemberAddr'")
    public Boolean saveMemberAddr(MemberAddr memberAddr) {
        //获取当前用户的openid
        String openId = AuthUtils.getMemberOpenId();
        //设置用户的openid
        memberAddr.setOpenId(openId);
        //设置地址状态
        memberAddr.setStatus(1);
        //设置默认地址
        memberAddr.setCommonAddr(0);
        //设置创建时间
        memberAddr.setCreateTime(new Date());
        //设置更新时间
        memberAddr.setUpdateTime(new Date());
        //保存配送地址
        return memberAddrMapper.insert(memberAddr) == 1;
    }

    /**
     * 修改配送地址
     * @param memberAddr 配送地址
     * @return 结果
     */
    @Override
    @CacheEvict(key = "'queryAllMemberAddr'")
    public Boolean modifyMemberAddr(MemberAddr memberAddr) {
        return memberAddrMapper.updateById(memberAddr) > 0;
    }


}




